home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / rogue / message.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-13  |  5.5 KB  |  327 lines

  1. /*
  2.  * message.c
  3.  *
  4.  * This source herein may be modified and/or distributed by anybody who
  5.  * so desires, with the following restrictions:
  6.  *    1.)  No portion of this notice shall be removed.
  7.  *    2.)  Credit shall not be taken for the creation of this source.
  8.  *    3.)  This code is not to be traded, sold, or used for personal
  9.  *         gain or profit.
  10.  *
  11.  */
  12.  
  13. #ifndef CURSES
  14. #include <curses.h>
  15. #endif CURSES
  16. #include <stdio.h>
  17. #include "rogue.h"
  18.  
  19. char msg_line[DCOLS] = "";
  20. short msg_col = 0;
  21. boolean msg_cleared = 1;
  22. char hunger_str[8] = "";
  23.  
  24. extern boolean cant_int, did_int, interrupted, save_is_interactive;
  25. extern short add_strength;
  26. extern short cur_level;
  27.  
  28. message(msg, intrpt)
  29. char *msg;
  30. boolean intrpt;
  31. {
  32.     if (!save_is_interactive) {
  33.         return;
  34.     }
  35.     if (intrpt) {
  36.         interrupted = 1;
  37.         md_slurp();
  38.     }
  39.     cant_int = 1;
  40.  
  41.     if (!msg_cleared) {
  42.         mvaddstr(MIN_ROW-1, msg_col, MORE);
  43.         refresh();
  44.         wait_for_ack();
  45.         check_message();
  46.     }
  47.     (void) strcpy(msg_line, msg);
  48.     mvaddstr(MIN_ROW-1, 0, msg);
  49.     addch(' ');
  50.     refresh();
  51.     msg_cleared = 0;
  52.     msg_col = strlen(msg);
  53.  
  54.     if (did_int) {
  55.         onintr();
  56.     }
  57.     cant_int = 0;
  58. }
  59.  
  60. remessage()
  61. {
  62.     if (msg_line[0]) {
  63.         message(msg_line, 0);
  64.     }
  65. }
  66.  
  67. check_message()
  68. {
  69.     if (msg_cleared) {
  70.         return;
  71.     }
  72.     move(MIN_ROW-1, 0);
  73.     clrtoeol();
  74.     refresh();
  75.     msg_cleared = 1;
  76. }
  77.  
  78. get_input_line(prompt, insert, buf, if_cancelled, add_blank, do_echo)
  79. char *prompt, *buf, *insert;
  80. char *if_cancelled;
  81. boolean add_blank;
  82. boolean do_echo;
  83. {
  84.     short ch;
  85.     short i = 0, n;
  86.  
  87.     message(prompt, 0);
  88.     n = strlen(prompt);
  89.  
  90.     if (insert[0]) {
  91.         mvaddstr(0, n + 1, insert);
  92.         (void) strcpy(buf, insert);
  93.         i = strlen(insert);
  94.         move(0, (n + i + 1));
  95.         refresh();
  96.     }
  97.  
  98.     while (((ch = rgetchar()) != '\r') && (ch != '\n') && (ch != CANCEL)) {
  99.         if ((ch >= ' ') && (ch <= '~') && (i < MAX_TITLE_LENGTH-2)) {
  100.             if ((ch != ' ') || (i > 0)) {
  101.                 buf[i++] = ch;
  102.                 if (do_echo) {
  103.                     addch(ch);
  104.                 }
  105.             }
  106.         }
  107.         if ((ch == '\b') && (i > 0)) {
  108.             if (do_echo) {
  109.                 mvaddch(0, i + n, ' ');
  110.                 move(MIN_ROW-1, i+n);
  111.             }
  112.             i--;
  113.         }
  114.         refresh();
  115.     }
  116.     check_message();
  117.     if (add_blank) {
  118.         buf[i++] = ' ';
  119.     } else {
  120.         while ((i > 0) && (buf[i-1] == ' ')) {
  121.             i--;
  122.         }
  123.     }
  124.  
  125.     buf[i] = 0;
  126.  
  127.     if ((ch == CANCEL) || (i == 0) || ((i == 1) && add_blank)) {
  128.         if (if_cancelled) {
  129.             message(if_cancelled, 0);
  130.         }
  131.         return(0);
  132.     }
  133.     return(i);
  134. }
  135.  
  136. rgetchar()
  137. {
  138.     register ch;
  139.  
  140.     for(;;) {
  141.         ch = getchar();
  142.  
  143.         switch(ch) {
  144.         case '\022':
  145.             wrefresh(curscr);
  146.             break;
  147. #ifdef UNIX_BSD4_2
  148.         case '\032':
  149.             printf(CL);
  150.             fflush(stdout);
  151.             tstp();
  152.             break;
  153. #endif UNIX_BSD4_2
  154.         case 'X':
  155.             save_screen();
  156.             break;
  157.         default:
  158.             return(ch);
  159.         }
  160.     }
  161. }
  162. /*
  163. Level: 99 Gold: 999999 Hp: 999(999) Str: 99(99) Arm: 99 Exp: 21/10000000 Hungry
  164. 0    5    1    5    2    5    3    5    4    5    5    5    6    5    7    5
  165. */
  166.  
  167. print_stats(stat_mask)
  168. register stat_mask;
  169. {
  170.     char buf[16];
  171.     boolean label;
  172.     int row = DROWS - 1;
  173.  
  174.     label = (stat_mask & STAT_LABEL) ? 1 : 0;
  175.  
  176.     if (stat_mask & STAT_LEVEL) {
  177.         if (label) {
  178.             mvaddstr(row, 0, "Level: ");
  179.         }
  180.         /* max level taken care of in make_level() */
  181.         sprintf(buf, "%d", cur_level);
  182.         mvaddstr(row, 7, buf);
  183.         pad(buf, 2);
  184.     }
  185.     if (stat_mask & STAT_GOLD) {
  186.         if (label) {
  187.             if (rogue.gold > MAX_GOLD) {
  188.                 rogue.gold = MAX_GOLD;
  189.             }
  190.             mvaddstr(row, 10, "Gold: ");
  191.         }
  192.         sprintf(buf, "%d", rogue.gold);
  193.         mvaddstr(row, 16, buf);
  194.         pad(buf, 6);
  195.     }
  196.     if (stat_mask & STAT_HP) {
  197.         if (label) {
  198.             mvaddstr(row, 23, "Hp: ");
  199.             if (rogue.hp_max > MAX_HP) {
  200.                 rogue.hp_current -= (rogue.hp_max - MAX_HP);
  201.                 rogue.hp_max = MAX_HP;
  202.             }
  203.         }
  204.         sprintf(buf, "%d(%d)", rogue.hp_current, rogue.hp_max);
  205.         mvaddstr(row, 27, buf);
  206.         pad(buf, 8);
  207.     }
  208.     if (stat_mask & STAT_STRENGTH) {
  209.         if (label) {
  210.             mvaddstr(row, 36, "Str: ");
  211.         }
  212.         if (rogue.str_max > MAX_STRENGTH) {
  213.             rogue.str_current -= (rogue.str_max - MAX_STRENGTH);
  214.             rogue.str_max = MAX_STRENGTH;
  215.         }
  216.         sprintf(buf, "%d(%d)", (rogue.str_current + add_strength),
  217.             rogue.str_max);
  218.         mvaddstr(row, 41, buf);
  219.         pad(buf, 6);
  220.     }
  221.     if (stat_mask & STAT_ARMOR) {
  222.         if (label) {
  223.             mvaddstr(row, 48, "Arm: ");
  224.         }
  225.         if (rogue.armor && (rogue.armor->d_enchant > MAX_ARMOR)) {
  226.             rogue.armor->d_enchant = MAX_ARMOR;
  227.         }
  228.         sprintf(buf, "%d", get_armor_class(rogue.armor));
  229.         mvaddstr(row, 53, buf);
  230.         pad(buf, 2);
  231.     }
  232.     if (stat_mask & STAT_EXP) {
  233.         if (label) {
  234.             mvaddstr(row, 56, "Exp: ");
  235.         }
  236.         /*  Max exp taken care of in add_exp() */
  237.         sprintf(buf, "%d/%D", rogue.exp, rogue.exp_points);
  238.         mvaddstr(row, 61, buf);
  239.         pad(buf, 11);
  240.     }
  241.     if (stat_mask & STAT_HUNGER) {
  242.         mvaddstr(row, 73, hunger_str);
  243.         clrtoeol();
  244.     }
  245.     refresh();
  246. }
  247.  
  248. pad(s, n)
  249. char *s;
  250. short n;
  251. {
  252.     short i;
  253.  
  254.     for (i = strlen(s); i < n; i++) {
  255.         addch(' ');
  256.     }
  257. }
  258.  
  259. save_screen()
  260. {
  261.     FILE *fp;
  262.     short i, j, row, col;
  263.     char buf[DCOLS+2];
  264.     boolean found_non_blank;
  265.  
  266.     row = curscr->_cury;
  267.     col = curscr->_curx;
  268.  
  269.     if ((fp = fopen("rogue.screen", "w")) != NULL) {
  270.         for (i = 0; i < DROWS; i++) {
  271.             found_non_blank = 0;
  272.             for (j = (DCOLS - 1); j >= 0; j--) {
  273.                 buf[j] = mvinch(i, j);
  274.                 if (!found_non_blank) {
  275.                     if ((buf[j] != ' ') || (j == 0)) {
  276.                         buf[j + ((j == 0) ? 0 : 1)] = 0;
  277.                         found_non_blank = 1;
  278.                     }
  279.                 }
  280.             }
  281.             fputs(buf, fp);
  282.             putc('\n', fp);
  283.         }
  284.         fclose(fp);
  285.     } else {
  286.         sound_bell();
  287.     }
  288.     move(row, col);
  289.     refresh();
  290. }
  291.  
  292. sound_bell()
  293. {
  294.     putchar(7);
  295.     fflush(stdout);
  296. }
  297.  
  298. boolean
  299. is_digit(ch)
  300. short ch;
  301. {
  302.     return((ch >= '0') && (ch <= '9'));
  303. }
  304.  
  305. r_index(str, ch, last)
  306. char *str;
  307. int ch;
  308. boolean last;
  309. {
  310.     int i = 0;
  311.  
  312.     if (last) {
  313.         for (i = strlen(str) - 1; i >= 0; i--) {
  314.             if (str[i] == ch) {
  315.                 return(i);
  316.             }
  317.         }
  318.     } else {
  319.         for (i = 0; str[i]; i++) {
  320.             if (str[i] == ch) {
  321.                 return(i);
  322.             }
  323.         }
  324.     }
  325.     return(-1);
  326. }
  327.